local class = reload("class")
local super = reload("super.basic")
local timer = reload("single.timer")
local randLcg = reload("random.lcg")

local this = class(super)

function this:ctor()
    super.ctor(self)
end

---清理操作数据
function this:clearWork(flgCamp, sort, obj, arg)
    if not obj:isWork() then
        return
    end
    obj:workClear(arg)
end

---检查自动操作
function this:checkWork(flgCamp, sort, obj, arg)
    if not obj:isWork() then
        return
    end
    obj:workProc(arg)
end

---超时自动操作
function this:entrust(arg, round)
    ---本轮超时检查
    if arg.round == round then
        return
    end
    ---阵营自动操作
    arg.bt:for2Camp(self.checkWork, self, arg)
    ---下一步操作
    self:coback(arg)
end

---灵敏比较函数
local function compareDex(a, b)
    return a.dex > b.dex
end

---收集灵敏数据
function this:sortDexs(arg, camp, obj)
    table.insert(arg.dex, {
        obj = obj,
        dex = obj:getDex()
    })
end

---计算出手顺序
function this:sortShot(arg)
    arg.dex = {}
    arg.bt:for2Camp(self.sortDexs, self, arg)
    table.sort(arg.dex, compareDex)
end

---计算本轮操作
function this:execShot(arg)
    for _, v in ipairs(arg.dex) do
        v.obj:shop(arg)
    end
end

---广播本轮结构
function this:broadcastShot(arg)
end

function this:mainProc(arg)
    ---本轮初始化
    arg.bt:for2Camp(self.clearWork, self, arg)
    ---本轮超托管
    local fmt = "battle_move:%d"
    local key = fmt:format(arg.id)
    timer:secFirstBy(key, arg.roundSec, self.entrust, arg, arg.round)
    self:await()
    ---计算出手顺序
    self:sortShot(arg)
    ---执行运算处理
    self:execShot(arg)
    ---广播本轮结果
    self:broadcastShot(arg)
end

return this
